package com.example.springboot.service;

import com.example.springboot.dao.UserRepository;
import com.example.springboot.vo.UserVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * service层
 * <p>
 *
 * @author Zhang.Z
 * @version 2025-09-22
 */
@Service
public class UserService {

    private final UserRepository userRepository;

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    /**
     * 查询一条
     */
    public void find(Long id) {
        Optional<UserVO> byId = userRepository.findById(id);
        if (byId.isPresent()) {
            System.out.println(byId.get().toString());
        } else {
            System.out.println("ID not found.");
        }
    }

    /**
     * 查询所有
     */
    public void findAll() {
        List<UserVO> all = userRepository.findAll();
        System.out.println("list size : " + all.size());
        if (!all.isEmpty()) {
            System.out.println("first user :" + all.get(all.size()-1).toString());
        }
    }
}
